﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using fivescomplement.Models;

namespace fivescomplement.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            if (isUserAuth() > 0)
            {
                if (Convert.ToInt32(Session["Type"]) == 3)
                {
                    using (var database = new C335263_teacherharvestEntities())
                    {
                        string Reference = Convert.ToString(Session["Reference"]);
                        var Adminuser = database.GetQuestionsAdmin(Reference);

                        return View(Adminuser.ToList());
                    }
                }
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }
            return View();
        }

        public int isUserAuth()
        {
            int returnvalue = -1;
            String authUser2 = User.Identity.Name;
            if (authUser2.Equals("") || Convert.ToInt32(Session["Type"]) != 3)
            {
            }
            else
            {
                returnvalue = 1;
            }
            return returnvalue;
        }

        public ActionResult Delete(int id, int id2)
        {
            if (isUserAuth() > 0)
            {
                using (var database = new C335263_teacherharvestEntities())
                {
                    database.DeleteDefaultQuestion(id, id2);
                }
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }

            return RedirectToAction("Index", new { id = id });
        }

        public ActionResult Questions(int id)
        {
            if (isUserAuth() > 0)
            {

                if (Convert.ToInt32(Session["Type"]) == 3)
                {
                    Session["InstitutionID"] = id;
                    using (var database = new C335263_teacherharvestEntities())
                    {
                        var Adminuser = database.GetDefaultQuestionSets();

                        return View(Adminuser.ToList());
                    }
                }
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }
            return View();
        }

        [HttpPost]
        public ActionResult Questions(FormCollection form, string returnUrl)
        {
            if (isUserAuth() > 0)
            {
                if (Convert.ToInt32(Session["Type"]) == 3)
                {
                    using (var database = new C335263_teacherharvestEntities())
                    {
                        int defaultquestionsetid = Convert.ToInt32(form["QuestionSetRadioButton"]);
                        int InstitutionID = Convert.ToInt32(form["InstitutionID"]);
                        database.InsertDefaultQuestionSets(InstitutionID, defaultquestionsetid);
                    }
                }
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }
            return RedirectToAction("Index", new { id = form["InstitutionID"] });
        }


        public ActionResult Create(int id)
        {
            if (isUserAuth() > 0)
            {
                ViewData["InstitutionID"] = id;
                return View();
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }
        }

        [HttpPost]
        public ActionResult Create(QuestionModel model, string returnUrl)
        {
            if (isUserAuth() > 0)
            {
                using (var database = new C335263_teacherharvestEntities())
                {
                    if ((model.Question + "").Length > 0)
                    {
                        database.CreateDefaultQuestion(model.CourseID , model.Question, model.QuestionTypeID);
                    }
                }
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }

            return RedirectToAction("Index", new { id = model.CourseID });
        }


    }
}
